package cn.iocoder.yudao.module.wine.controller.admin.leaveinfo;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.wine.controller.admin.leaveinfo.vo.*;
import cn.iocoder.yudao.module.wine.dal.dataobject.leaveinfo.LeaveInfoDO;
import cn.iocoder.yudao.module.wine.service.leaveinfo.LeaveInfoService;

@Tag(name = "管理后台 - 发布离职通告")
@RestController
@RequestMapping("/wine/leave-info")
@Validated
public class LeaveInfoController {

    @Resource
    private LeaveInfoService leaveInfoService;

    @PostMapping("/create")
    @Operation(summary = "创建发布离职通告")
    @PreAuthorize("@ss.hasPermission('wine:leave-info:create')")
    public CommonResult<Integer> createLeaveInfo(@Valid @RequestBody LeaveInfoSaveReqVO createReqVO) {
        return success(leaveInfoService.createLeaveInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "审核发布离职通告")
    @PreAuthorize("@ss.hasPermission('wine:leave-info:update')")
    public CommonResult<Boolean> updateLeaveInfo(@Valid @RequestBody LeaveInfoSaveReqVO updateReqVO) {
        leaveInfoService.updateLeaveInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除发布离职通告")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('wine:leave-info:delete')")
    public CommonResult<Boolean> deleteLeaveInfo(@RequestParam("id") Integer id) {
        leaveInfoService.deleteLeaveInfo(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得发布离职通告")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('wine:leave-info:query')")
    public CommonResult<LeaveInfoRespVO> getLeaveInfo(@RequestParam("id") Integer id) {
        LeaveInfoDO leaveInfo = leaveInfoService.getLeaveInfo(id);
        return success(BeanUtils.toBean(leaveInfo, LeaveInfoRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得发布离职通告分页")
    @PreAuthorize("@ss.hasPermission('wine:leave-info:query')")
    public CommonResult<PageResult<LeaveInfoRespVO>> getLeaveInfoPage(@Valid LeaveInfoPageReqVO pageReqVO) {
        PageResult<LeaveInfoRespVO> pageResult = leaveInfoService.getLeaveInfoPage(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出发布离职通告 Excel")
    @PreAuthorize("@ss.hasPermission('wine:leave-info:export')")
    @OperateLog(type = EXPORT)
    public void exportLeaveInfoExcel(@Valid LeaveInfoPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<LeaveInfoRespVO> list = leaveInfoService.getLeaveInfoPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "发布离职通告.xls", "数据", LeaveInfoRespVO.class,
                        BeanUtils.toBean(list, LeaveInfoRespVO.class));
    }

}
